/*
 * @Author: 秦福凯
 * @LastEditTime: 2025-02-06 17:33:24
 * @Description: 一些常用的js方法封装
 */


// 脱敏函数封装 包括 手机号 身份证 银行卡
export function desensitization(text,type){
  //  type phone 手机 idCard 身份证 bankCard 银行卡 email 邮箱 name 姓名
  let type2 = type ? type :'phone'
  switch(type2){
    case 'phone':
    return text.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2')
    break

    case 'idCard':
    return text.replace(/(\d{6})\d{8}(\d{4})/, '$1********$2');
    break

    case 'bankCard':
    return text.replace(/(\d{6})\d{8}(\d{4})/, '$1********$2');
    break

    case 'email':
    const [prefix, domain] = text.split('@');
    const maskedPrefix = prefix.replace(/(.{2}).+(.{1})/, '$1***$2');
    return `${maskedPrefix}@${domain}`;
    break

    case 'name':
    if (text.length <= 1) return text;
    return text[0] + '*'.repeat(text.length - 1);
    break
  }

}



// 日期格式化
export function parseTime(time, pattern) {
  if (arguments.length === 0 || !time) {
    return null
  }
  const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
      time = parseInt(time)
    } else if (typeof time === 'string') {
      time = time.replace(new RegExp(/-/gm), '/').replace('T', ' ').replace(new RegExp(/\.[\d]{3}/gm), '');
    }
    if ((typeof time === 'number') && (time.toString().length === 10)) {
      time = time * 1000
    }
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
    if (result.length > 0 && value < 10) {
      value = '0' + value
    }
    return value || 0
  })
  return time_str
}



